package cn.yunyichina.provider.clinic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.yunyichina.provider.clinic.dao.ClinicRecordDao;
import cn.yunyichina.provider.clinic.entity.ClinicRecord;
import cn.yunyichina.provider.clinic.service.OrderService;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.base.OrderExtVo;
import cn.yunyichina.provider.iface.entity.clinic.ClinicOrder;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;

@Service
public class OrderServiceImpl implements OrderService {

	private Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private ClinicRecordDao clinicRecordDao;
	
    /**
     * 用于异常退费流程、原渠道退费后，更新订单状态。
     */
	@Override
    public ResCommon<Void> updateClinicOrder(ClinicOrder co) {
        ResCommon<Void> response = new ResCommon<Void>();

        Map<String, String> dataMap = new HashMap<String, String>();
        dataMap.put("order_no", co.getOrderNo());
        dataMap.put("org_id", co.getOrgId());
        Response ordResponse = BaseService.callUnderlineFromInternal("", "yyt.base.order.by.order.no.out.get",
                JsonUtils.toJsonString(dataMap));
        if (!"0".equals(ordResponse.getResultCode())) {
            response.setResultCodeAndMessage("3002", "找不到云医订单号[" + co.getOrderNo() + "]对应的门诊订单信息！");
            return response;
        }
        OrderExtVo vo = JsonUtils.parseObject(ordResponse.getResult(), OrderExtVo.class);

        OrderExtVo updateOrderExtVo = new OrderExtVo();
        updateOrderExtVo.setId(vo.getId());
        updateOrderExtVo.setOrderStatus((short) co.getOrderStatus().intValue());
        updateOrderExtVo.setPayStatus((short) co.getPayStatus().intValue());
        updateOrderExtVo.setUpdateTime(DateUtils.dateToString(new Date()));
        updateOrderExtVo.setRefundTime(DateUtils.dateToString(new Date()));
        logger.info("updateClinicOrder, updateOrderExtVo=" + JSONObject.toJSONString(updateOrderExtVo));
        BaseService.callUnderlineFromInternal("", "yyt.base.order.update", JsonUtils.toJsonString(updateOrderExtVo));

        response.setResultCodeAndMessage("0", "云医订单号[" + co.getOrderNo() + "]更新退款状态成功！");
        return response;
    }

}
